import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { h } from 'vue';

import { DictEnum } from '@vben/constants';
import { getPopupContainer } from '@vben/utils';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'name',
    label: '收藏夹名称',
  },
  {
    component: 'Input',
    fieldName: 'description',
    label: '描述',
  },
  {
    component: 'Select',
    // formItemClass: 'col-span-2',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.FAVORITE_OPEN_STATUS, true),
      class: 'w-full',
    },
    fieldName: 'openStatus',
    label: '开放状态', // (1:公开,2:指定人公开,3:自己可见)
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
      class: 'w-full',
    },
    fieldName: 'status',
    label: '状态',
  },
  {
    component: 'UserListSelect',
    fieldName: 'createBy',
    label: '创建人',
  },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '收藏夹名称',
    field: 'name',
  },
  {
    title: '描述',
    field: 'description',
  },
  {
    title: '开放状态',
    field: 'openStatus',
    width: 150,
    slots: {
      default: ({ row }) => {
        return renderDict(row.openStatus, DictEnum.FAVORITE_OPEN_STATUS);
      },
    },
  },
  {
    field: 'status',
    title: '状态',
    width: 90,
    slots: { default: 'status' },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    label: '主键',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '收藏夹名称',
    fieldName: 'name',
    component: 'Input',
    componentProps: {
      showCount: true,
      maxlength: 20,
      placeholder:
        '格式建议为YYYYMMdd-用途描述 (YYYY为年份，MM为月份，dd为日期)，如20251001-观众开发',
    },
    help: '收藏夹名称格式建议为YYYYMMdd-用途描述 (YYYY为年份，MM为月份，dd为日期)，如20251001-观众开发',
    rules: 'required',
  },
  {
    label: '描述',
    fieldName: 'description',
    component: 'Textarea',
    componentProps: {
      autoSize: { minRows: 3 },
    },
    help: '备注内容，用于描述收藏夹的作用，易于用户区分',
    // rules: 'required',
  },
  {
    label: '开放状态',
    fieldName: 'openStatus',
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.FAVORITE_OPEN_STATUS, true),
      optionType: 'button',
    },
    help: '公开：所有人可查看；\n指定人公开：所指定的用户可查看；\n仅自己可见：仅创建者可见',
    rules: 'selectRequired',
  },
  {
    label: '指定人可见',
    fieldName: 'specifiedUserIds',
    component: 'UserListSelect',
    componentProps: {
      mode: 'multiple',
    },
    defaultValue: [],
    dependencies: {
      show: (values) => values?.openStatus === 2,
      triggerFields: ['openStatus'],
    },
    help: '指定的人员才能查看此收藏夹',
    rules: 'required',
  },
  {
    label: '状态',
    fieldName: 'status',
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
      optionType: 'button',
    },
    rules: 'selectRequired',
  },
];

export const distinctDrawerSchema: FormSchemaGetter = () => [
  {
    label: '已选收藏夹',
    fieldName: 'favoriteIds',
    component: 'TagList',
    componentProps: { tagList: [] },
    formItemClass: 'items-start',
  },
  {
    component: h('span', { class: 'text-sm' }, ''),
    fieldName: 'repeatCount',
    label: '重复项数量',
  },
  {
    label: '保留目标至',
    fieldName: 'retainFavoriteId',
    component: 'CompanyFavoriteSelect',
    componentProps: {
      getFavoriteList: false,
      favorites: [],
    },
    help: '请选择一个收藏夹保留去重后的唯一数据',
    rules: 'selectRequired',
  },
  {
    component: 'CompanyFavoriteSelect',
    fieldName: 'targetFavoriteId',
    label: '处理重复项',
    componentProps: {
      allowClear: true,
    },
    help: '是否将这些重复项另行复制到其他收藏夹中？（可选）',
  },
];

export const mergeDrawerSchema: FormSchemaGetter = () => [
  {
    label: '已选收藏夹',
    fieldName: 'favoriteIds',
    component: 'TagList',
    componentProps: { tagList: [] },
    formItemClass: 'items-start',
  },
  {
    label: '合并方式',
    fieldName: 'operationType',
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.FAVORITE_MERGE_WAY, true),
      optionType: 'button',
    },
    help: '复制：将所有数据复制合并至新的收藏夹，所选的收藏夹仍保留不变；\n移入并删除原收藏夹：将所有数据复制合并至新的收藏夹，并清空删除所选的收藏夹',
    rules: 'selectRequired',
  },
  {
    component: 'Divider',
    fieldName: '_divider',
    formItemClass: 'col-span-3',
    hideLabel: true,
    renderComponentContent: () => {
      return {
        default: () => h('div', '新收藏夹'),
      };
    },
  },
  ...drawerSchema(),
];

export const detailsSchema: DetailsModalSchemaGetter = () => [
  {
    label: '收藏夹名称',
    fieldName: 'name',
    copy: true,
  },
  {
    label: '收藏夹描述',
    fieldName: 'description',
    textWrap: true,
  },
  {
    label: '开放状态',
    fieldName: 'openStatus',
    dictEnum: DictEnum.FAVORITE_OPEN_STATUS,
  },
  {
    label: '指定人',
    fieldName: 'specifiedUserIds',
    custom: true,
    dependencies: {
      if: (values) => values?.openStatus === 2,
    },
  },
  {
    label: '状态',
    fieldName: 'status',
    dictEnum: DictEnum.SYS_NORMAL_DISABLE,
  },
];
